import tkinter as tk
from datetime import datetime, timedelta

# 获取当前日期
current_date = datetime.now().date()

# 定义各个时间段
start_time = datetime.combine(
    current_date, datetime.strptime("09:00:00", "%H:%M:%S").time()
)
lunch_break_start = datetime.combine(
    current_date, datetime.strptime("12:00:00", "%H:%M:%S").time()
)
lunch_break_end = datetime.combine(
    current_date, datetime.strptime("13:30:00", "%H:%M:%S").time()
)
end_time = datetime.combine(
    current_date, datetime.strptime("18:00:00", "%H:%M:%S").time()
)


# 上次更新时间
last_update_time = datetime.now()


# 计算剩余时间
def calculate_remaining_time(current_time):
    if current_time < start_time:
        return start_time - current_time, "上班剩余时间"
    elif current_time < lunch_break_start:
        return lunch_break_start - current_time, "上午上班剩余"
    elif current_time < lunch_break_end:
        return lunch_break_end - current_time, "午休结束剩余"
    elif current_time < end_time:
        return end_time - current_time, "下午下班剩余"
    else:
        return timedelta(seconds=0), "已经下班"


# 更新倒计时
def update_countdown():
    global last_update_time
    current_time = datetime.now().replace(microsecond=0)
    if current_time.minute == 0 and current_time.second == 0:
        last_update_time = current_time
    elif (current_time - last_update_time).seconds > 3600:
        last_update_time = current_time
    remaining_time, message = calculate_remaining_time(current_time)
    formatted_time = str(remaining_time).split(".")[0]
    hours, minutes, seconds = formatted_time.split(":")
    label.config(
        text=f"{message}: {hours.zfill(2)}:{minutes.zfill(2)}:{seconds.zfill(2)}"
    )
    if (current_time - last_update_time).seconds <= 3600:
        root.after(1000, update_countdown)  # 每秒更新一次
    else:
        root.after(3600000, update_countdown)  # 每小时更新一次


# 开始移动窗口
def start_move(event):
    global x_start, y_start
    x_start = event.x
    y_start = event.y


# 停止移动窗口
def stop_move(event):
    pass  # 结束移动时可以不执行任何操作


# 在拖动过程中移动窗口
def do_move(event):
    delta_x = event.x - x_start
    delta_y = event.y - y_start
    geom = f"+{root.winfo_x() + delta_x}+{root.winfo_y() + delta_y}"
    root.geometry(geom)


# 关闭窗口
def close_window():
    root.destroy()


# 创建主窗口
root = tk.Tk()
root.title("下班倒计时")
root.overrideredirect(True)  # 移除窗口边框和标题栏
root.attributes("-alpha", 0.5)  # 设置窗口透明度（0.0 到 1.0）
root.configure(bg="white")  # 设置窗口背景颜色

# 设置窗口初始位置到屏幕右上角
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
window_width = 300  # 设置窗口宽度
window_height = 100  # 设置窗口高度
root.geometry(f"{window_width}x{window_height}+{screen_width - window_width}+0")

# 禁止调整窗口大小
root.resizable(False, False)

# 创建标签用于显示倒计时
label = tk.Label(root, text="", font=("Arial", 15), bg="white")
label.pack(pady=20)

# 创建关闭按钮
close_button = tk.Button(
    root,
    text="关闭",
    command=close_window,
    font=("Arial", 14),
    bg="red",
    fg="white",
    width=10,
    height=1,
)
close_button.pack(pady=5)

# 绑定鼠标事件
root.bind("<ButtonPress-1>", start_move)
root.bind("<ButtonRelease-1>", stop_move)
root.bind("<B1-Motion>", do_move)

# 启动倒计时
update_countdown()

# 运行主循环
root.mainloop()
